package com.longshihan.carservice.controller;

import com.github.pagehelper.PageInfo;
import com.longshihan.carservice.http.APIResponse;
import com.longshihan.carservice.model.ArticleDomain;
import com.longshihan.carservice.service.Article.ArticleService;
import com.longshihan.carservice.utils.GsonUtils;
import com.longshihan.carservice.utils.UUID;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author longhe
 * @time
 */
@Api("文章相关")
@Controller
@RequestMapping("/article")
public class ArticleController extends BaseController {


    @Autowired
    private ArticleService articleService;


    @ApiOperation(value = "新增文章",notes = "json转义添加")
    @PostMapping(value = "/addarticle")
    @ResponseBody
    public APIResponse saveGoods(HttpServletRequest request,  HttpServletResponse response,
                                 @ApiParam(name = "article", value = "文章", required = true)
                                 @RequestParam(name = "article", required = true) String articleJson,
                                 @ApiParam(name = "authorId", value = "用户ID", required = true)
                                     @RequestParam(name = "authorId", required = true) String authorId) {
        try{
            ArticleDomain domain= (ArticleDomain) GsonUtils.JSONToObject(articleJson,ArticleDomain.class);
            if (domain==null){
                String msg = "文章序列化失败";
                return APIResponse.fail(msg);
            }
            domain.setAuthorId(authorId);
            domain.setArticleId(UUID.UU64());
            articleService.addArticle(domain);
            return APIResponse.success();
        }catch (Exception e){
            String msg = "添加文章失败";
            return APIResponse.fail(e.getMessage());
        }
    }



    @ApiOperation(value = "更新文章",notes = "更新文章")
    @PostMapping(value = "/updateArticle")
    @ResponseBody
    public APIResponse saveGoodsBean(HttpServletRequest request,  HttpServletResponse response,
                                 @ApiParam(name = "authorId", value = "用户ID", required = true) @RequestParam(name = "authorId", required = true) String authorId,
                                 @ApiParam(name = "article", value = "文章", required = true) @RequestParam(name = "article", required = true) String article
    ) {
        try{
            ArticleDomain domain= (ArticleDomain) GsonUtils.JSONToObject(article,ArticleDomain.class);
            if (domain==null){
                String msg = "更新文章序列化失败";
                return APIResponse.fail(msg);
            }
            articleService.updateArticle(domain);
            return APIResponse.success();
        }catch (Exception e){
            String msg = "更新文章失败";
            return APIResponse.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "删除文章",notes = "删除文章")
    @PostMapping(value = "/delArticle")
    @ResponseBody
    public APIResponse delArticleById(HttpServletRequest request,  HttpServletResponse response,
                                     @ApiParam(name = "authorId", value = "用户ID", required = true) @RequestParam(name = "authorId", required = true) String authorId,
                                     @ApiParam(name = "articleId", value = "文章ID", required = true) @RequestParam(name = "articleId", required = true) String articleId
    ) {
        try{
            articleService.delArticle(authorId,articleId);
            return APIResponse.success();
        }catch (Exception e){
            String msg = "删除文章失败";
            return APIResponse.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "查询文章",notes = "查询单个文章")
    @PostMapping(value = "/getArticle")
    @ResponseBody
    public APIResponse getArticleById(HttpServletRequest request,  HttpServletResponse response,
                                    @ApiParam(name = "authorId", value = "用户ID", required = true) @RequestParam(name = "authorId", required = true) String authorId,
                                    @ApiParam(name = "articleId", value = "文章ID", required = true) @RequestParam(name = "articleId", required = true) String articleId
    ) {
        try{
           ArticleDomain ArticleDomain= articleService.getArticleById(authorId,articleId);
           if (ArticleDomain!=null){
               return APIResponse.success(GsonUtils.toJsonString(ArticleDomain));
           }else {
               return APIResponse.fail("获取不到文章信息");
           }

        }catch (Exception e){
            String msg = "查询文章失败";
            return APIResponse.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "查询全部文章",notes = "查询全部文章")
    @PostMapping(value = "/getArticles")
    @ResponseBody
    public APIResponse getAllArticleByUserId(HttpServletRequest request,  HttpServletResponse response,
                                    @ApiParam(name = "pageNum", value = "页面数",defaultValue = "1") @RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
                                    @ApiParam(name = "pagesize", value = "单次查询的个数", required = true,defaultValue = "20") @RequestParam(name = "pagesize", required = true,defaultValue = "20") int pagesize
    ) {
        try{
            PageInfo<ArticleDomain> articleDomainPageInfo=articleService.getArticles(pageNum,pagesize);
            if (articleDomainPageInfo!=null){
                return APIResponse.success(GsonUtils.toJsonString(articleDomainPageInfo));
            }else {
                return APIResponse.fail("获取不到文章信息");
            }

        }catch (Exception e){
            String msg = "查询文章失败";
            return APIResponse.fail(e.getMessage());
        }
    }

}
